package us.nchu.tomtiger.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import us.nchu.tomtiger.enums.ResultEnum;
import us.nchu.tomtiger.model.Collection;
import us.nchu.tomtiger.model.Project;
import us.nchu.tomtiger.model.Result;
import us.nchu.tomtiger.model.User;
import us.nchu.tomtiger.service.CollectionService;
import us.nchu.tomtiger.service.ProjectService;
import us.nchu.tomtiger.service.UserService;
import us.nchu.tomtiger.service.UserShowService;
import us.nchu.tomtiger.utils.ResultUtil;
import us.nchu.tomtiger.utils.UserUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by XiongXuan on 2018/1/7.
 */
@Controller
@RequestMapping("/center")
public class CenterController {
    @Resource
    private UserShowService userShowService;
    @Resource
    private UserService userService;
    @Resource
    private ProjectService projectService;
    @Resource
    private CollectionService collectionService;

    @GetMapping
    public ModelAndView getCenter() {
        return new ModelAndView("center");
    }

    @GetMapping("/publish")
    public ModelAndView getCreatProject() {
        return new ModelAndView("publish");
    }

    @GetMapping("/modify")
    public ModelAndView getModify(HttpServletRequest request) {
        User user =  userService.getUserByPhoneNumber(UserUtil.getUserPhoneNumber(request));

        ModelAndView modelAndView = new ModelAndView("modify");

        // 是否是刚注册
        String firstRegister = (String)request.getSession().getAttribute("firstRegister");
        if (firstRegister != null) {
            modelAndView.addObject("firstRegister", true);
            request.getSession().removeAttribute("firstRegister");
        }

        request.getSession().setAttribute("originalUserType", Integer.toString(user.getUserType()));

        System.out.println(user);

        modelAndView.addObject("phoneNumber", user.getPhoneNumber());
        modelAndView.addObject("nickName", user.getNickName());
        modelAndView.addObject("userType", user.getUserType());
        modelAndView.addObject("contactType", user.getContact_type());
        modelAndView.addObject("contact", user.getContact());
        modelAndView.addObject("intro", user.getIntro());
        modelAndView.addObject("major", user.getMajor());
        modelAndView.addObject("schoolName", user.getSchoolName());

        return modelAndView;
    }

    @GetMapping("/collection")
    public ModelAndView getCollection(HttpServletRequest httpServletRequest) {
        ModelAndView modelAndView = new ModelAndView("collection");

        String phoneNumber = UserUtil.getUserPhoneNumber(httpServletRequest);

        List<Collection> collections = collectionService.getListByPhoneNumber(phoneNumber);
//        for (int i = 0; i < collections.size(); i++) {
//            System.out.println(collections.get(i));
//        }
        List<Project> projects = projectService.getProjectListByCollectionList(collections);
//        for (int i = 0; i < projects.size(); i++) {
//            System.out.println(projects.get(i));
//        }

        modelAndView.addObject("list", projects);

        return modelAndView;
    }

    @DeleteMapping("/collection")
    @ResponseBody
    public Result incollectProject(HttpServletRequest httpServletRequest, String projectId) {
        if (!collectionService.isCollected(UserUtil.getUserPhoneNumber(httpServletRequest), Integer.parseInt(projectId))) {
            return ResultUtil.getNoObjectInstance(ResultEnum.ERROR_COLLECTION_NO_COLLECT);
        }
        collectionService.deleteCollection(UserUtil.getUserPhoneNumber(httpServletRequest)
                , projectId);

        return ResultUtil.success("取消收藏成功");
    }

    @PutMapping("/collection")
    @ResponseBody
    public Result collectProject(HttpServletRequest httpServletRequest, int projectId) {
        if (collectionService.isCollected(UserUtil.getUserPhoneNumber(httpServletRequest), projectId)) {
            return ResultUtil.getNoObjectInstance(ResultEnum.ERROR_COLLECTION_ALREADY_COLLECT);
        }

        collectionService.createCollection(new Collection(UserUtil.getUserPhoneNumber(httpServletRequest), projectId));

        return ResultUtil.success("收藏成功");
    }
}
